<?php
$GLOBALS['db'] = null;

function connectToDB()
{
    $con= mysqli_connect("localhost","root","","actsat");


   
// Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    return $con;
}

function connectToDBPersistant()
{

    if($GLOBALS['db'] == null)
    {
        $GLOBALS['db'] = mysqli_connect("localhost","root","","actsat");
    }

   
// Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    return $GLOBALS['db'];
}

function getUsernameFromId($userID)
{
    $sql = connectToDB();
    
    $query = $sql->query("SELECT `username` FROM `users` WHERE `id` = '$userID'") or die(mysqli_error($sql));
    $result = $query->fetch_object();

    /* ok we're done, let's close our connection */
    $sql->close();

    return $result->username;
}

function getUserName()
{
   return getUsernameFromId (getSessionUserID());  
}

function isLoggedIn()
{
    return getSessionUserID() != null;
}

function getSessionUserID()
{
    if (isset($_SESSION['u_id']))
    {
        return $_SESSION['u_id'];
    }
    
    return null;    
}

function setSessionUserID($id)
{
    $_SESSION['u_id'] = $id;
}

function logout()
{
    unset($_SESSION['u_id']);
    header('Location: index.php');
}

function logUserIn($username, $password, $columnToUse)
{
    $sql = connectToDB();
    
    $query =  $sql->query("SELECT `pass`, `id` FROM `users` WHERE `".$columnToUse."` = '$username'") or die(mysqli_error($sql));
    

    $result = $query->fetch_object();

    /* ok we're done, let's close our connection */
    $sql->close();
    
    if ($result)
    {
        if (strcmp($result->pass, md5(md5($password)) == 0))
        {
            return $result->id;
        }
    }   
        
    return -1;
}

function emailHasBeenTaken($email)
{
    $sql = connectToDB();
    $result = $sql->query("SELECT COUNT(*) FROM `users` WHERE `email` = '$email'");
    $row = $result->fetch_row();
    
    return $row[0] != 0;
}

function usernameHasBeenTaken($username)
{
    $sql = connectToDB();
    $result = $sql->query("SELECT COUNT(*) FROM `users` WHERE `username` = '$username'");
    $row = $result->fetch_row();
    
    return $row[0] != 0;
}

function getCorrectAnswerFor($testId, $section, $questionNumber)
{    
    $con = connectToDBPersistant();
    $result = $con->query("SELECT `correctAnswer`, `id` FROM `acttestquestions` WHERE `testID` = '$testId' AND `sectionID` = '$section' AND `questionNumber` = '$questionNumber'");
    $row = $result->fetch_row();
    
    return $row;
}



function insertQuestionAttempt($testAttemptID, $questionID, $selectedAnswer, $isCorrect)
{
   $con = connectToDBPersistant();
    
    if ($isCorrect)
    {
        $isCorrect = 1;
    }
    else
    {
        $isCorrect = 0;
    }
    
    // Zero is false, anything else is true
    $stmt = $con->prepare("INSERT INTO questionattempts(testattemptid, questionid, selectedanswer, iscorrect) VALUES(?,?,?,?)");
    $stmt->bind_param('iisi', $testAttemptID, $questionID, $selectedAnswer, $isCorrect );
    $stmt->execute(); 
}

function getTotalScoreForSection($section, $testAttemptID)
{
    $con = connectToDB();
    
    
    $result = $con->query("SELECT COUNT(*) "
            . "FROM  `questionattempts` INNER JOIN `acttestquestions` ON questionattempts.questionid = acttestquestions.id "
            . "WHERE questionattempts.testattemptid = '$testAttemptID' && acttestquestions.sectionID = '$section' && questionattempts.iscorrect = 1") 
            or die(mysqli_error($con));
    
    $row = $result->fetch_row();
    
    return $row[0];
}

function getTestIdFromTestAttempt($attemptId)
{
    $con = connectToDB();
    $result = $con->query("SELECT `testid`, `id` FROM `testattempts` WHERE `id` = '$attemptId'");
    $obj = $result->fetch_object();
    
    return $obj->testid;
}

function getDateForTestAttempt($attemptId)
{
    $con = connectToDB();
    $result = $con->query("SELECT `date` FROM `testattempts` WHERE `id` = '$attemptId'");
    $obj = $result->fetch_object();
    
    return $obj->date;
}

function getAnswerArrayFor($testId, $section)
{    
    $con = connectToDBPersistant();
    $result = $con->query("SELECT `correctAnswer`, `id` FROM `acttestquestions` WHERE `testID` = '$testId' AND `sectionID` = '$section' ORDER BY `questionNumber` ASC");
   
    $resultData = array();
    $questionIds = array();
    
    while ($row= $result->fetch_row())
    {
        $resultData[] = $row[0];
        $questionIds[] = $row[1];
    }
    
    
    $return = array();
    
    $return[] = $resultData;
    $return[] = $questionIds;
    
    
    return $return;
}

function getSelectedAnswerFor($testAttemptId, $questionId)
{    
    $con = connectToDBPersistant();
    $result = $con->query("SELECT `selectedanswer` FROM `questionattempts` WHERE `testattemptid` = '$testAttemptId' AND `questionid` = '$questionId'");
   
    $row= $result->fetch_row();

    
    return $row[0];
}


function getScaledEnglishScore($score)
{
    switch($score)
    {
        case 0:
        case 1:
            return 1;
            
        case 2:
        case 3: 
            return 2;
            
        case 4:
        case 5:
            return 3;
            
        case 6:
        case 7:
             return 4;
            
        case 8:
        case 9:
             return 5;
            
        case 10:
        case 11:
            return 6;
            
        case 12:
        case 13:
        case 14:
            return 7;
            
        case 15:
        case 16:
        case 17:
            return 8;
            
        case 18:
        case 19:
        case 20:
            return 9;
            
        case 21:
        case 22:
        case 23:
            return 10;
            
        case 24:
        case 25:
            return 11;
            
        case 26:
        case 27:
            return 12;
            
        case 28:
        case 29:
            return 13;
            
        case 30:
        case 31:
        case 32:
            return 14;
            
        case 33:
        case 34:
        case 35:
            return 15;
            
        case 36:
        case 37:
            return 16;
            
        case 38:
        case 39:
            return 17;
            
        case 40:
        case 41:
        case 42:
            return 18;
            
        case 43:
        case 44:
        case 45:
            return 19;
            
        case 46:
        case 47:
        case 48:
            return 20;
            
        case 49:
        case 50:
        case 51:  
            return 21;
            
        case 52:
        case 53:
            return 22;
            
        case 54:
        case 55:
            return 23;
            
        case 56:
        case 57:
            return 24;
            
        case 58:
        case 59:
        case 60:
            return 25;
            
        case 61:
        case 62:
            return 26;
            
        case 63:
        case 64:
            return 27;
        case 65:
        case 66:
            return 28;
            
        case 67:
            return 29;
            
        case 68:
        case 69:
            return 30;
            
        case 70:
            return 31;
            
        case 71:
            return 32;
            
        case 72:
            return 33;
            
        case 73:
            return 34;
            
        case 74:
            return 35;
            
        case 75:
            return 36;
            
        default:
            return 0;
    }
}

function getScaledMathScore($score)
{
    switch($score)
    {
        case 0:
            return 1;
            
        case 1:
            return 4;
            
        case 2:
            return 6;

        case 3:
            return 8;
            
        case 4:
            return 9;
            
        case 5:
            return 10;
            
        case 6:
            return 11;
            
        case 7:            
        case 8:
            return 12;
            
        case 9:
        case 10:            
        case 11:
            return 13;
            
        case 12:
        case 13:
        case 14:
            return 14;
            
        case 15:
        case 16:
        case 17:
            return 15;
            
        case 18:
        case 19:
        case 20:
            return 16;
            
        case 21:
        case 22:
        case 23:
        case 24:
            return 17;
            
        case 25:
        case 26:
        case 27:
            return 18;
            
        case 28:
        case 29:
            return 19;
            
        case 30:
        case 31:
            return 20;
            
        case 32:
        case 33:
            return 21;
            
        case 34:
        case 35:
            return 22;
            
        case 36:
        case 37:
            return 23;
            
        case 38:
        case 39:
        case 40:
            return 24;
            
        case 41:
        case 42:
            return 25;
            
        case 43:
        case 44:
            return 26;
            
        case 45:
        case 46:
        case 47:
            return 27;
            
        case 48:
        case 49:
            return 28;
            
        case 50:
        case 51:
            return 29;
            
        case 52:
        case 53:
            return 30;
            
        case 54:
            return 31;
            
        case 55:
        case 56:
            return 32;
            
        case 57:
            return 33;
            
        case 58:
            return 34;
            
        case 59:
            return 35;
            
        case 60:
            return 36;
            
        default: 
            return 0;
    }
}

function getScaledReadingScore($score)
{
    switch($score)
    {
        case 0:
            return 1;
            
        case 1:
            return 2;
            
        case 2:
            return 4;
            
        case 3:
            return 5;
            
        case 4:
            return 7;
            
        case 5:
            return 8;
            
        case 6:
            return 9;
            
        case 7:
            return 10;
            
        case 8:
        case 9:
            return 11;
            
        case 10:
        case 11:
            return 12;
            
        case 12:
        case 13:
            return 13;
            
        case 14:
            return 14;
            
        case 15:
        case 16:
            return 15;
            
        case 17:
            return 16;
            
        case 18:
            return 17;
            
        case 19:
            return 18;
            
        case 20:
            return 19;
            
        case 21:
        case 22:
            return 20;
            
        case 23:
            return 21;
            
        case 24:
            return 22;
            
        case 25:
        case 26:
            return 23;
            
        case 27:
            return 24;
            
        case 28:
        case 29:
            return 25;
            
        case 30:
            return 26;
            
        case 31:
            return 27;
            
        case 32:
        case 33:
            return 28;
            
        case 34:
            return 29;
            
        case 35:
            return 30;
            
        case 36:
            return 31;
            
        case 37:
            return 32;
            
        case 38:
            return 34;
            
        case 39:
            return 35;
            
        case 40:
            return 36;
            
        default:
            return 0;
    }
}

function getScaledScienceScore($score)
{
    switch($score)
    {
        case 0:
            return 1;
            
        case 1:
            return 3;
            
        case 2:
            return 5;
            
        case 3:
            return 6;
            
        case 4:
            return 7;
            
        case 5:
            return 8;
            
        case 6:
            return 9;
            
        case 7:
        case 8:
            return 10;
            
        case 9:
            return 11;
            
        case 10:
            return 12;
            
        case 11:
        case 12:
            return 13;
            
        case 13:
            return 14;
            
        case 14:
            return 15;
            
        case 15:
        case 16:
            return 16;
            
        case 17:
        case 18:
            return 17;
            
        case 19:
        case 20:
            return 18;
            
        case 21:
        case 22:
            return 19;
            
        case 23:
        case 24:
            return 20;
            
        case 25:
        case 26:
            return 21;
            
        case 27:
            return 22;
            
        case 28:
        case 29:
            return 23;
            
        case 30:
            return 24;
            
        case 31:
        case 32:
            return 25;
            
        case 33:
            return 26;
            
        case 34:
            return 27;
            
        case 35:
            return 28;
        case 36:
            return 29;
            
        case 37:
            return 30;
            
        case 38:
            return 32;
            
        case 39:
            return 34;
            
        case 40:
            return 36;
            
        default: 
            return 0;
    }
}

function getMonthName($month)
{
    switch($month)
    {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3: 
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            return null;
    }
}